﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.固定资产
{
    public partial class Form固定资产一览 : DevExpress.XtraEditors.XtraForm
    {
        private List<AssetsDTO> assetsDTOs = new List<AssetsDTO>();
        private AssetsDTO assetsDTO = new AssetsDTO();
        private int curPage = 1;
        public Form固定资产一览()
        {
            InitializeComponent();
            this.xtraUserControl分页.williamPagerEvent += this.refreshGrid;
            this.refreshGrid(1);
        }

        public void refreshGrid(int curPage)
        {
            this.curPage = curPage;
            this.assetsDTOs = 固定资产Service.getAssets((this.curPage - 1).ToString(), "100");
            this.gridControl固定资产一览.DataSource = this.assetsDTOs;
        }

        private void simpleButton新建_Click(object sender, EventArgs e)
        {
            new Form固定资产创建(固定资产Service.getNewAssetsNo()).ShowDialog();
        }

        private void textBox固定资产条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.assetsDTOs.Clear();
                this.assetsDTOs.Add(固定资产Service.getAssetsDTOByAssertNo(this.textBox固定资产条码.Text.Trim()));
                this.gridControl固定资产一览.DataSource = this.assetsDTOs;
            }
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.assetsDTO = 固定资产Service.getAssetsDTOByAssertNo(
                FunctionHelper.GetgridViewValue(
                    this.gridView固定资产一览,
                    this.gridView固定资产一览.FocusedRowHandle, 
                    "assertNo"
                    )
                );
            if (String.IsNullOrEmpty(this.assetsDTO.assertNo))
            {
                MessageBox.Show("无资产信息");
                return;
            }
            //if (!File.Exists(@"tag\.btw"))
            //{
            //    PictureService.DownloadPicture(URL.图片url + URL.打印模板路径 + ".btw", @"tag\" + ".btw", -1);
            //}
            Engine btEngine = new Engine();
            btEngine.Start();
            //LabelFormatDocument btFormat = btEngine.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory + @"tag\.btw");
            LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\固定资产贴.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            //btFormat.PrintSetup.PrinterName = "PDFCreator";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值

            btFormat.SubStrings["assertNo"].Value = this.assetsDTO.assertNo;
            btFormat.SubStrings["name"].Value = this.assetsDTO.name;
            btFormat.SubStrings["band"].Value = this.assetsDTO.band;
            btFormat.SubStrings["usage"].Value = this.assetsDTO.usage;
            btFormat.SubStrings["ownerShip"].Value = this.assetsDTO.ownerShip;
            btFormat.SubStrings["vendorName"].Value = this.assetsDTO.vendorName;
            btFormat.SubStrings["contract"].Value = this.assetsDTO.contract;
            btFormat.SubStrings["mobile"].Value = this.assetsDTO.mobile;

            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Seagull.BarTender.Print.Result nResult1 = btFormat.Print("固定资产贴" + "", waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }
    }
}
